package com.hellpro.lolbox.acticity.presenter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.hellpro.lolbox.acticity.model.RedBiz;
import com.hellpro.lolbox.acticity.view.AudioActivity;
import com.hellpro.lolbox.acticity.view.IAudioActivity;
import com.hellpro.lolbox.acticity.view.ICommandFragment;
import com.hellpro.lolbox.acticity.view.ILiveLinkActivity;
import com.hellpro.lolbox.acticity.view.IMainActivity;
import com.hellpro.lolbox.acticity.view.IRedActivity;
import com.hellpro.lolbox.acticity.view.LiveVideoActivity;
import com.hellpro.lolbox.acticity.view.NormalActivity;
import com.hellpro.lolbox.acticity.view.RedActivity;
import com.hellpro.lolbox.acticity.view.VideoLinkActivity;
import com.hellpro.lolbox.utils.UIManger;

import java.io.File;
import java.io.IOException;

import static android.widget.Toast.makeText;

/**
 * 类描述:
 * 创建人:阿犇
 * 创建时间:2016/10/21
 * 备注:主页面与数据交互的类
 */

public class MainActivityPresenter {

    private IMainActivity mIMainActivity;
    private IRedActivity mIRedActivity;
    private RedBiz redBiz;
    private ICommandFragment mICommandFragment;
    private Context mContext;
    private IAudioActivity mIAudioActivity;
    private MediaRecorder mediaRecorder;
    private ILiveLinkActivity mILiveLinkActivity;

    public MainActivityPresenter(IMainActivity iMainActivity) {
        this.mIMainActivity = iMainActivity;
    }

    public MainActivityPresenter(IRedActivity iRedActivity, Context context) {
        redBiz = new RedBiz();
        this.mIRedActivity = iRedActivity;
        this.mContext = context;
    }

    public MainActivityPresenter(ICommandFragment iCommandFragment) {
        this.mICommandFragment = iCommandFragment;
    }

    public MainActivityPresenter(IAudioActivity iAudioActivity) {
        this.mIAudioActivity = iAudioActivity;
    }

    public MainActivityPresenter(ILiveLinkActivity iLiveLinkActivity) {
        this.mILiveLinkActivity = mILiveLinkActivity;
    }


    /**
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/21
     * 备注:实现六个功能
     */

    public void startContent() {

        //设置图文动态的跳转
        mIMainActivity.getNormal().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIManger.startToActivity(mIMainActivity.getMainContext(), NormalActivity.class);
            }
        });
        //设置红包的跳转
        mIMainActivity.getRed().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIManger.startToActivity(mIMainActivity.getMainContext(), RedActivity.class);
            }
        });
        //设置看视频的跳转
        mIMainActivity.getVideo().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
                mIMainActivity.getMainContext().startActivity(intent);
            }
        });
        //设置语音动态的跳转
        mIMainActivity.getAudio().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIManger.startToActivity(mIMainActivity.getMainContext(), AudioActivity.class);
            }
        });
        //设置直播的跳转
        mIMainActivity.getLive().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIManger.startToActivity(mIMainActivity.getMainContext(), LiveVideoActivity.class);
            }
        });
        //设置视频链接的跳转
        mIMainActivity.getLink().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIManger.startToActivity(mIMainActivity.getMainContext(), VideoLinkActivity.class);
            }
        });

    }

    /**
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/21
     * 备注:设置红包页面的内容
     */


    public void setRedViewPager() {
        redBiz.setViewpager(mIRedActivity.getVpFragmentManager(), mIRedActivity.getTitles(), mIRedActivity.getFragments(), mIRedActivity.getRedViewPager());
    }

    /**
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/21
     * 备注:发红包
     */

    public void setPayRed() {
        final EditText command = mICommandFragment.getCommand();
        final EditText count = mICommandFragment.getCount();
        final EditText yanBao = mICommandFragment.getYanBao();
        final Button btnPay = mICommandFragment.getBtnPay();

        String commandStr = command.getText().toString();
        String countStr = count.getText().toString();
        String yuanbaoStr = yanBao.getText().toString();
        if (commandStr.isEmpty() && countStr.isEmpty() && yuanbaoStr.isEmpty()) {
            btnPay.setBackgroundColor(Color.RED);
            btnPay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(mICommandFragment.getMainContext());
                    builder.setTitle("提示").setMessage("真的要支付吗").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            makeText(mICommandFragment.getMainContext(), "暂时无法完成支付", Toast.LENGTH_SHORT).show();
                        }
                    }).setNegativeButton("取消", null).show();
                }
            });
        } else {
            btnPay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    makeText(mICommandFragment.getMainContext(), "数据不完善", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }


    /**
     * 类描述:
     * 创建人:阿犇
     * 创建时间:2016/10/21
     * 备注:设置录音的方法
     */

    public void setAudio() {
        Button ivAudio = mIAudioActivity.getIvAudio();

        ivAudio.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        //不能再监听外面初始化MediaRecorder，huidaozhi没有释放报错
                        mediaRecorder = new MediaRecorder();
                        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
                        String path = Environment.getExternalStorageDirectory() + File.separator + "test.mp3";
                        mediaRecorder.setOutputFile(path);
                        /**
                         * 调用start开始录音之前，一定要调用prepare方法。
                         */
                        try {
                            mediaRecorder.prepare();
                            mediaRecorder.start();
                            Toast toast = Toast.makeText(mIAudioActivity.getAudioContent(), "按住不松即可开始录音", Toast.LENGTH_SHORT);
                            toast.setGravity(Gravity.CENTER,0,0);
                            toast.show();
                        } catch (IllegalStateException | IOException e) {
                            e.printStackTrace();
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        mediaRecorder.stop();
                        Toast toast = Toast.makeText(mIAudioActivity.getAudioContent(), "录音结束", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER,0,0);
                        toast.show();
                        mediaRecorder.release();
                        mediaRecorder = null;
                        break;
                }
                return true;
            }
        });

        //播放音频
        ImageView ivPlay = mIAudioActivity.getIvPlay();
        ivPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path = Environment.getExternalStorageDirectory() + File.separator + "test.mp3";
                MediaPlayer player = new MediaPlayer();
                try {
                    player.setDataSource(path);
                    player.prepare();
                    player.start();
                    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            makeText(mIAudioActivity.getAudioContent(), "音频播放完毕", Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (IOException e) {
                    Log.e("TAG", "播放失败");
                }
            }
        });

    }
    //设置视频链接页面的监听
    public void setLiveLinkListener(){
        mILiveLinkActivity.getIvBack().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }


}
